package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.domain.YddAdvimg;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.domain.YddAdvpos;
import com.itheima.youdianda.dto.PageDto;
import com.itheima.youdianda.dto.YddAdvimgDto;
import com.itheima.youdianda.dto.YddAdvposDto;
import com.itheima.youdianda.service.YddAdvimgService;
import com.itheima.youdianda.service.YddAdvposService;
import com.itheima.youdianda.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("ydd_advimg")
public class YddAdvimgController {
    @Autowired
    private YddAdvimgService yddAdvimgService;
    @Autowired
    private YddAdvposService yddAdvposService;

    @GetMapping("/rowInfo")
    public R<YddAdvimg> page(@RequestHeader("x-token") String token, int id) {
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            YddAdvimg byId = yddAdvimgService.getById(id);
            return R.success(byId);
        }
        return R.error("查询失败");
    }

    @GetMapping("/list")
    public R<Page> page(@RequestHeader("x-token") String token, int page, int limit, String advimgdesc, String advimgpos) {
        boolean flag = JwtUtils.verifyToken(token);
        if (flag) {
            PageDto<YddAdvimg> pageInfo = yddAdvimgService.page( page, limit,  advimgdesc, advimgpos);
            return R.success(pageInfo);
        }
        return R.success(null,"查询失败");

    }
    @PostMapping("/add")
    public R<String> save(@RequestBody YddAdvimg yddAdvimg) {
        log.info(yddAdvimg.toString());
        yddAdvimgService.savexinxinxi(yddAdvimg);
        return R.success("广告图增加成功");
    }

    @PostMapping("/update")
    public R<String> get(@RequestBody YddAdvimg yddAdvimg) {
        yddAdvimgService.updateById(yddAdvimg);
        return R.success("信息已成功修改");
    }

    @GetMapping("del")
    public R<String> delete(Integer id) {
        yddAdvimgService.removeById(id);
        return R.success(null, "删除成功");
    }


}
